//
//  DesktopPolicy.m
//  gdemm
//
//  Created by 乔明超 on 2017/6/14.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "DesktopPolicy.h"
#import "Policies.h"
#import "UserDefaultsUtil.h"
#import "StringUtil.h"

@implementation DesktopPolicy

static DesktopPolicy * manager;

+(instancetype)getInstence{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (NSString *)supportPolicy{
    return DESKTOP_MANAGER;
}

- (BOOL)startPolicyWithPolicyType:(NSString *)policyType policyContent:(NSDictionary *)policyContent{
    HRLog(@"policyType = %@ policyContent = %@",policyType,policyContent);
    if (policyContent != nil) {
        NSArray *appList = policyContent[@"appList"];
        NSMutableString *apps = [NSMutableString string];
        for (int i = 0; i < appList.count; i++) {
            NSString *app = appList[i];
            HRLog(@"app = %@",app);
            if (![apps containsString:app]) {
                [apps appendFormat:@"%@;",app];
            }
        }
        HRLog(@"apps = %@",apps);
        [[UserDefaultsUtil shareInstance]saveStringConfig:LC_DESKTOPAPPS :apps];
        if (![StringUtil isEmpty:apps]) {
            [[UserDefaultsUtil shareInstance]saveBoolConfig:LC_USEGDLAUNCHER :YES];
        }
    }
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"deskupdate" object:nil];
    return false;
}

- (BOOL)stopPolicyWithPolicyType:(NSString *)policyType{

    [[UserDefaultsUtil shareInstance]saveBoolConfig:LC_USEGDLAUNCHER :NO];
    [[UserDefaultsUtil shareInstance]saveStringConfig:LC_DESKTOPAPPS :@""];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"deskupdate" object:nil];
    return NO;
}
@end
